use XML::RSS::Parser::Lite;
use LWP::Simple;
use XML::Simple;

use Data::Dumper;

my $last_year = 2011;

while (<STDIN>) {
  chomp $_;
  my $blog = $_;
  my %p = get_profile($blog);


 if ($p{created}->{y} < 1995) {
   print STDERR "wrong created->year for $blog\n";
   next;
 }

  my $y = $p{created}->{y};
  for (; $y <= $last_year; $y++) {
    for (my $m = 1; $m <= 12; $m++) {
      #print "downloading $blog for $y - $m\n";
#      search_posts($blog, $y, $m);
    }
  }

}

#get_profile("lazy-frog.livejournal.com");
#get_profile("fat-crocodile.livejournal.com");

sub search_posts {
  my ($blog, $year, $month) = @_;
  $month =~ s/^(\d)$/0$1/;
  my $url = "http://$blog/$year/$month/";
  $doc = get($url);
#  print STDERR $doc . "\n---\n";
  while ($doc =~ /$blog\/([\d+]+)\.html/g) {
    print "POST\t$year\t$month\t$blog/" . $1 . ".html\n";
  }  
}

sub get_profile {
  my $blog_url = shift;
  my %h;
  my $xml = get("http://" . $blog_url . "/data/foaf");

  my $foaf = XMLin($xml);

  my $dateCreated = $foaf->{"foaf:Person"}->{"foaf:weblog"}->{"lj:dateCreated"};
  if ($dateCreated =~ /^(\d{4,4})-(\d{2,2})-(\d{2,2})/) {
    ($h{created}->{y}, $h{created}->{m}, $h{created}->{d}) = ($1, $2, $3);
  } else {
    print STDERR "can't parse foaf:Person / foaf:weblog / ljCreated: \"$dateCreated\"\n";
  }

  if (exists $foaf->{"foaf:Person"}->{"ya:country"}->{"dc:title"}) {
    $h{"Автор:Гео"} = $foaf->{"foaf:Person"}->{"ya:country"}->{"dc:title"};

    if (exists $foaf->{"foaf:Person"}->{"ya:city"}->{"dc:title"}) {
      $h{"Автор:Гео"} .= "/" . $foaf->{"foaf:Person"}->{"ya:city"}->{"dc:title"};
    }
  }

  if (exists $foaf->{"foaf:Person"}->{"lj:journaltitle"}) {
    $h{"ЖЖ:Заголовок"} = $foaf->{"foaf:Person"}->{"lj:journaltitle"};
  }
  
  if (exists $foaf->{"foaf:Person"}->{"lj:journalsubtitle"}) {
    $h{"ЖЖ:Подзаголовок"} = $foaf->{"foaf:Person"}->{"lj:journalsubtitle"};
  }

  if (exists $foaf->{"foaf:Person"}->{"foaf:name"}) {
    $h{"Автор:Имя"} = $foaf->{"foaf:Person"}->{"foaf:name"};
  }

  if (exists $foaf->{"foaf:Person"}->{"foaf:dateOfBirth"}) {
    my $dateOfBirth = $foaf->{"foaf:Person"}->{"foat:dateOfBirth"};
    if ($dateOfBirth =~ /^(\d{4,4})-(\d{2,2})-(\d{2,2})/) {
      $h{"Автор:ГодРождения"} = $1;
      $h{"Автор:ДатаРождения"} = $3 . "/" . $2;
    }
  }
 
  print Dumper(\%h); 
  return %h;
}
